"use strict"

let CBSliderMap = {}
function CeateSlider(slideLink)
{
    if(!slideLink.id){
      slideLink.id = "CBSlider_edvd_" + Math.floor(Math.random()*10000);
    }
    var id = slideLink.id;

    CBSliderMap[id] = {};
    CBSliderMap[id].hsObj    = $("#"+id);
    var hsdata = CBSliderMap[id].hsObj.attr("data");
    if(hsdata!=null&&hsdata!=undefined){
      var array = hsdata.split(":");
      CBSliderMap[id].minPer   = Number.parseInt(array[0]);
      CBSliderMap[id].maxPer   = Number.parseInt(array[1]);
      CBSliderMap[id].px       = Number.parseInt(array[2]);
      CBSliderMap[id].widthRef = $("#"+array[3]);
    }
    CBSliderMap[id].moveDistant = 0;
    CBSliderMap[id].isMouseDown = false;
    CBSliderMap[id].slideContainer = document.querySelector("#" + id + " .CBScroll");

    var hsObj = $(slideLink).find(".CBScroll");
    $(hsObj)[0].onmousedown = onmousedown;
    $(hsObj)[0].onmousemove   = onmousemove;
    $(hsObj)[0].onmouseup   = onmouseup;

    var mycard2 = $(hsObj).find("div");
    for(var jj=0;jj<mycard2.length;jj++){
      mycard2[jj].onmousedown = onmousedown
      mycard2[jj].onmousemove = onmousemove
      mycard2[jj].onmouseup = onmouseup
    }

    var mycard2 = $(hsObj).find("img");
    for(var jj=0;jj<mycard2.length;jj++){
      mycard2[jj].onmousedown = onmousedown
      mycard2[jj].onmousemove = onmousemove
      mycard2[jj].onmouseup = onmouseup
      $(mycard2[jj]).addClass("unselectable");
      $(mycard2[jj]).attr("draggable","false");
    }

    var mycard2 = $(hsObj).find("a");
    for(var jj=0;jj<mycard2.length;jj++){
      mycard2[jj].onmousedown = onmousedown
      mycard2[jj].onmousemove = onmousemove
      mycard2[jj].onmouseup = onmouseup
    }
}

let moveDistant = 0;
let isMouseDown = false;
let idDrag = false;
let MouseDownCBS = null;

//PC端——拖动移动图片
onmousedown = function(e) {
    isMouseDown = true;
    moveDistant = e.clientX;
    if(MouseDownCBS==null){
      MouseDownCBS = $(e.currentTarget).parents(".CBScrollable").find(".CBScroll")[0];
      if(MouseDownCBS){
        $(MouseDownCBS).attr("data",MouseDownCBS.scrollLeft);
      }
    }
}

onmousemove  = function(e) {
    if(isMouseDown==false)
      return;
    if(e.clientX - moveDistant < -1){//next
      card_scroll_next(moveDistant - e.clientX)
    }
    else if (e.clientX - moveDistant > 1){ //prev
      card_scroll_next(moveDistant - e.clientX)
    }
}

onmouseup = function(e) {
    if(isMouseDown==false)
      return;
    isMouseDown = false;
    if(e.clientX - moveDistant < -1){//next
      card_scroll_next(moveDistant-e.clientX)
    }
    else if (e.clientX - moveDistant > 1){ //prev
      card_scroll_next(moveDistant-e.clientX)
    }
    MouseDownCBS = null;
}

function minPerSlide(e)
{
  if(window.innerWidth<767){
    return CBSliderMap[ e.id ].minPer;
  }
  return CBSliderMap[ e.id ].maxPer;
}

function card_scroll_next(dis)
{
    if(MouseDownCBS==null)return;

    var curPosX = Number.parseInt($(MouseDownCBS).attr("data"));
    curPosX += dis;
    MouseDownCBS.scrollLeft = curPosX;
    idDrag = true;
}


async function setWidth(id){
  if(CBSliderMap[id]==undefined)return

  var slider = CBSliderMap[id];
  if(slider.widthRef==undefined)return;

  var CBCards = $(slider.slideContainer).find(".CBCard");
  for (let slideLink of CBCards) {
      $(slideLink).outerWidth( slider.widthRef.outerWidth() / minPerSlide( slider.hsObj[0] ) );
  }
  var imgs = $(slider.slideContainer).find("img");
  for (let slideLink of imgs) {
      $(slideLink).outerWidth( slider.widthRef.outerWidth() / minPerSlide( slider.hsObj[0] ) );
  }
}

async function setWidthALL(){
  await Helper.sleep(300);
    for( var id in CBSliderMap )
    {
        setWidth(id);
    };
}


(function($) {
    "use strict"

    jQuery(window).on('load',function () {
      'use strict'; 
      setTimeout(function(){
          var slideLinks = $(".CBScrollable");
          for (let slideLink of slideLinks) {
            CeateSlider( slideLink );
          }
          setWidthALL();
      }, 1000);
      
    });

    jQuery(window).on('resize',function(){
      setWidthALL();
    });

})(jQuery);


